package cn.xz.zysjz.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import cn.xz.zysjz.R;
import cn.xz.zysjz.activity.HaoActivity;
import cn.xz.zysjz.activity.HistActivity;
import cn.xz.zysjz.activity.IndexActivity;
import cn.xz.zysjz.activity.MyActivity;
import cn.xz.zysjz.activity.SouActivity;
import cn.xz.zysjz.activity.WebActivity;
import cn.xz.zysjz.adapter.HomeListAdapter;
import cn.xz.zysjz.bean.ActivityListBean;
import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.bean.MessageEvent;
import cn.xz.zysjz.databinding.FragmentHomeBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.GetDataUtils;
import cn.xz.zysjz.utils.OkHttpUtils;

/**
 * 首页模块*/
public class HomeFragment extends Fragment {

    private FragmentHomeBinding mBinding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //最新消息列表处理
        initRV();
        //点击按钮事件
        initEvent();
    }

    private void initEvent(){
        //点击置顶
        mBinding.ivTop.setOnClickListener(v->{
            mBinding.rcHomeList.scrollToPosition(0);
        });
        //搜索
        mBinding.clTop.setOnClickListener(v->{
            AppHelper.toJump(SouActivity.class);
        });
    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new GridLayoutManager(this.getContext(),2);
        HomeListAdapter mAdapter = new HomeListAdapter(R.layout.item_home_list);
        mBinding.rcHomeList.setLayoutManager(ms);
        mBinding.rcHomeList.setAdapter(mAdapter);
        //设置列表的头布局
        View header = LayoutInflater.from(this.getContext()).inflate(R.layout.item_home_head, mBinding.rcHomeList, false);
        initHead(header);
        //设置头布局
        mAdapter.setHeaderView(header);
        //列表，每一条的点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                //跳转到详细界面
                ArrayList<HomeListBean> data = (ArrayList<HomeListBean>) adapter.getData();
                //跳转到详情页
                AppHelper.toJump(IndexActivity.class,data.get(position));
            }
        });
        mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (!CommonLib.isAdmin){
                    return false;
                }
                HomeListBean data = (HomeListBean) adapter.getData().get(position);
                ShowDialog.show1("确定删除吗?", getContext(), new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        GetDataUtils.delData(data.getNum());
                        adapter.remove(data);
                    }
                });
                return false;
            }
        });
        //进行网络请求，给列表赋值
        mAdapter.setList(GetDataUtils.get(0));
        //列表滑动事件，滑下去显示置顶图标
        mBinding.rcHomeList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //这里的目的是滑动列表，大于第三条的时候显示置顶图标
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager != null){
                    int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                    if (firstVisibleItemPosition > 3){
                        mBinding.ivTop.setVisibility(View.VISIBLE);
                    }else {
                        mBinding.ivTop.setVisibility(View.GONE);
                    }
                }

            }
        });
    }

    private void initHead(View view){
        //轮播图
        Banner banner = view.findViewById(R.id.banner_top);
        //顶部轮播图相关
        initBanner(banner);
        //预约挂号
        LinearLayoutCompat llcYy = view.findViewById(R.id.llc_yy);
        //快速挂号
        LinearLayoutCompat llcKs = view.findViewById(R.id.llc_ks);
        //预约取号
        LinearLayoutCompat llcQh = view.findViewById(R.id.llc_qh);
        //复诊预约
        LinearLayoutCompat llcFz = view.findViewById(R.id.llc_fz);
        //点击景点
        llcYy.setOnClickListener(v->{
            EventBus.getDefault().post(new MessageEvent("切换到大厅"));
        });
        //点击酒店推荐
        llcKs.setOnClickListener(v->{
            AppHelper.toJump(MyActivity.class);
        });
        //点击美食分享
        llcQh.setOnClickListener(v->{
            AppHelper.toJump(HaoActivity.class);
        });
        //点击美景民宿
        llcFz.setOnClickListener(v->{
            AppHelper.toJump(HistActivity.class);
        });
    }

    private void initBanner(Banner bannerTop){
        //轮播的图片列表
        ArrayList<Drawable> mList = new ArrayList<>();
        mList.add(AppHelper.getDrawable(R.mipmap.icon_b1));
        mList.add(AppHelper.getDrawable(R.mipmap.icon_b2));
        mList.add(AppHelper.getDrawable(R.mipmap.icon_b3));
        mList.add(AppHelper.getDrawable(R.mipmap.icon_b4));
        //设置适配器等轮播图信息
        bannerTop.setAdapter(new BannerImageAdapter<Drawable>(mList) {
                    @Override
                    public void onBindView(BannerImageHolder holder, Drawable data, int position, int size) {
                        //图片加载自己实现
                        holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        AppHelper.setPic(holder.imageView,data);
                    }
                })
                .setLoopTime(2000)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(this.getContext()));
    }
}
